{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5f2738f9",
   "metadata": {},
   "source": [
    "# 1.字典的初始（{key:value}）\n",
    "1. 字典的文档 (https://docs.python.org/3/tutorial/datastructures.html#dictionaries)\n",
    "2. 字典的重要属性：\n",
    "    A. 结构：key:value（键值对）\n",
    "     B. 字典是无序的，我们通过键(key)去查找值(value)，就好像list用索引\n",
    "     (index)查找值(value)一样，只不过字典的“索引”被定义了名字\n",
    "    C. 字典对key的定义有一定的要求：key是由\"不可变\"的数据结构才可以定\n",
    "     义，list是不能作为key来定义的。\n",
    "    D. 字典的key是唯一的，字典的值value也可以不唯一。（list的值是唯一的\n",
    "    么？）\n",
    "3. 字典的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c4548239",
   "metadata": {},
   "outputs": [],
   "source": [
    "users = [\n",
    "[\"zhichao\",23,\"IT\",20000],\n",
    "[\"xiaoming\",26,\"IT\",30000]\n",
    "]\n",
    "# 试对比： \n",
    "users_dict = {\n",
    "\"zhichao\":[23,\"IT\",20000],\n",
    "\"xiaoming\":[26,\"IT\",30000]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9160a965",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['xiaoming', 26, 'IT', 30000]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询xiaoming的信息： \n",
    "\n",
    "users[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bcc28cc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[26, 'IT', 30000]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users_dict[\"xiaoming\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "192e30ca",
   "metadata": {},
   "source": [
    "试问：如果users的数据很大，我想查其中的一个信息，但无法知道其索引值\n",
    "list就变得很无力，\n",
    "而dict只需要知道数据的关键信息即\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d75b811",
   "metadata": {},
   "source": [
    "# 2. 字典的创建(常见的三种创建方式)\n",
    "1. info = {key1:value1 , key2:value2 , ...}直接按照字典格式创建\n",
    "2. info_02 = dict(key1=value1 , key2=value2 , ... ) 使用dict()进行字典创建\n",
    "3. 运用字典增加的方式新建字典:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8fb0ad71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong', 'stu10004': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    " \"stu10001\":\"xiaoming\",\n",
    " \"stu10002\":\"lihua\",\n",
    " \"stu10003\":\"xiaohong\",\n",
    " \"stu10004\":\"xiaohong\",\n",
    "}\n",
    "print(info)\n",
    "print(type(info))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d21913be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_02 = dict(\n",
    "stu10001=\"xiaoming\",\n",
    "stu10002=\"lihua\", stu10003=\"xiaohong\",\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6e8316ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_03 = {} # 新建空字典\n",
    "info_03[\"stu10001\"] = \"xiaoming\"\n",
    "info_03[\"stu10002\"] = \"lihua\"\n",
    "info_03[\"stu10003\"] = \"xiaohong\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38b496c2",
   "metadata": {},
   "source": [
    "# 字典的增、删、改、查\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14e816fe",
   "metadata": {},
   "source": [
    "## 1. 字典的增加(和字典的第三种创建方式一致)："
   ]
  },
  {
   "cell_type": "raw",
   "id": "08b02460",
   "metadata": {},
   "source": [
    "info_03[\"stu10004\"] = \"zhichao\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74fda106",
   "metadata": {},
   "source": [
    "## 2. 字典的查询"
   ]
  },
  {
   "cell_type": "raw",
   "id": "04a47507",
   "metadata": {},
   "source": [
    "info_03[key_name]\n",
    "注意： 正确的查询方式，应该先查询key是否存在字典当中，如下："
   ]
  },
  {
   "cell_type": "raw",
   "id": "308b0d61",
   "metadata": {},
   "source": [
    "A. key_name in info_03 ## 查看是否存在,如果存在，\n",
    " 返回结果为 True ，不存在为 False\n",
    "B. info_03[key_name]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb3bbe83",
   "metadata": {},
   "source": [
    "## 3. 字典的修改"
   ]
  },
  {
   "cell_type": "raw",
   "id": "a69b88a0",
   "metadata": {},
   "source": [
    "A. 查询key是否存在\n",
    "B. 修改key的值\n",
    "\n",
    "例如： 修改info_03中stu10003的值为Alex, \n",
    "if\"stu10003\" in info_03 : info_03 [\"stu10003\"] =\"Alex\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ed734a3",
   "metadata": {},
   "source": [
    "## 4. 字典的删除"
   ]
  },
  {
   "cell_type": "raw",
   "id": "8958c776",
   "metadata": {},
   "source": [
    " pop(key) 2.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "28bb1e76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhichao'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info_03[\"stu10004\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2eb9911d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong', 'stu10004': 'zhichao'}\n"
     ]
    }
   ],
   "source": [
    "info_03 [\"stu10004\"] = \"zhichao\" \n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f900783d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'xiaohong'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用key查到value的数据 \n",
    "info_03[\"stu10003\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6d240035",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhichao'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info_03[\"stu10004\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c8728c65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "查询的key不存在\n"
     ]
    }
   ],
   "source": [
    "# 正确的查询字典方式\n",
    "print(\"stu10003\" in info_03)\n",
    "if \"stu10006\" in info_03:\n",
    " print(info_03[\"stu10006\"])\n",
    "else:\n",
    "    print(\"查询的key不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "62375621",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'Alex', 'stu10004': 'zhichao'}\n"
     ]
    }
   ],
   "source": [
    "# 修改: 也要先查询key\n",
    "if \"stu10003\" in info_03:\n",
    " info_03 [\"stu10003\"] = \"Alex\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a7449cb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10004': 'zhichao'}\n"
     ]
    }
   ],
   "source": [
    "# 字典的删除\n",
    "info_03.pop(\"stu10003\")\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "217d77e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua'}\n"
     ]
    }
   ],
   "source": [
    "# 字典的删除 popitem()\n",
    "info_03.popitem()\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "783dbd07",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on dict object:\n",
      "\n",
      "class dict(object)\n",
      " |  dict() -> new empty dictionary\n",
      " |  dict(mapping) -> new dictionary initialized from a mapping object's\n",
      " |      (key, value) pairs\n",
      " |  dict(iterable) -> new dictionary initialized as if via:\n",
      " |      d = {}\n",
      " |      for k, v in iterable:\n",
      " |          d[k] = v\n",
      " |  dict(**kwargs) -> new dictionary initialized with the name=value pairs\n",
      " |      in the keyword argument list.  For example:  dict(one=1, two=2)\n",
      " |  \n",
      " |  Built-in subclasses:\n",
      " |      StgDict\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      True if the dictionary has the specified key, else False.\n",
      " |  \n",
      " |  __delitem__(self, key, /)\n",
      " |      Delete self[key].\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(...)\n",
      " |      x.__getitem__(y) <==> x[y]\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __reversed__(self, /)\n",
      " |      Return a reverse iterator over the dict keys.\n",
      " |  \n",
      " |  __setitem__(self, key, value, /)\n",
      " |      Set self[key] to value.\n",
      " |  \n",
      " |  __sizeof__(...)\n",
      " |      D.__sizeof__() -> size of D in memory, in bytes\n",
      " |  \n",
      " |  clear(...)\n",
      " |      D.clear() -> None.  Remove all items from D.\n",
      " |  \n",
      " |  copy(...)\n",
      " |      D.copy() -> a shallow copy of D\n",
      " |  \n",
      " |  get(self, key, default=None, /)\n",
      " |      Return the value for key if key is in the dictionary, else default.\n",
      " |  \n",
      " |  items(...)\n",
      " |      D.items() -> a set-like object providing a view on D's items\n",
      " |  \n",
      " |  keys(...)\n",
      " |      D.keys() -> a set-like object providing a view on D's keys\n",
      " |  \n",
      " |  pop(...)\n",
      " |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      " |      If key is not found, d is returned if given, otherwise KeyError is raised\n",
      " |  \n",
      " |  popitem(self, /)\n",
      " |      Remove and return a (key, value) pair as a 2-tuple.\n",
      " |      \n",
      " |      Pairs are returned in LIFO (last-in, first-out) order.\n",
      " |      Raises KeyError if the dict is empty.\n",
      " |  \n",
      " |  setdefault(self, key, default=None, /)\n",
      " |      Insert key with a value of default if key is not in the dictionary.\n",
      " |      \n",
      " |      Return the value for key if key is in the dictionary, else default.\n",
      " |  \n",
      " |  update(...)\n",
      " |      D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.\n",
      " |      If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]\n",
      " |      If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v\n",
      " |      In either case, this is followed by: for k in F:  D[k] = F[k]\n",
      " |  \n",
      " |  values(...)\n",
      " |      D.values() -> an object providing a view on D's values\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  fromkeys(iterable, value=None, /) from builtins.type\n",
      " |      Create a new dictionary with keys from iterable and values set to value.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  __hash__ = None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fb9506c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
